package com.mdd.admin.controller.setting;

import com.mdd.admin.service.setting.ISettingWebsiteService;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.utils.ConfigUtil;
import com.mdd.common.utils.YmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 网站信息配置管理
 */
@RestController("settingWebsiteController")
@RequestMapping("api/setting/website")
public class WebsiteController {

    @Resource
    ISettingWebsiteService iSettingWebsiteService;

    /**
     * 获取网站配置信息
     *
     * @return Object
     * @author fzr
     */
    @GetMapping("/detail")
    public Object detail() {
        Map<String, String> detail = iSettingWebsiteService.detail();
        return AjaxResult.success(detail);
    }

    /**
     * 保存网站配置信息
     *
     * @param params 参数
     * @return Object
     * @author fzr
     */
    @PostMapping("/save")
    public Object save(@RequestBody Map<String, String> params) {
        iSettingWebsiteService.save(params);
        return AjaxResult.success();
    }

    /**
     * 获取地图的配置
     *
     * @param
     * @return Object
     * @author fzr
     */
    @GetMapping("/getMapKey")
    public Map<String, String> getMapKey() {
        String env = YmlUtil.get("like.production");
        boolean envStatus = StringUtils.isNotEmpty(env) && env.equals("true");
        Map<String, String> mapKey = ConfigUtil.get("website_map_key");
        String value = mapKey.get("tencent_map_key");
        value = envStatus ? "******" : value;
        mapKey.put("tencent_map_key", value);
        return mapKey;
    }

    /**
     * 设置地图的配置
     *
     * @param
     * @return Object
     * @author fzr
     */
    @PostMapping("/setMapKey")
    public void setMapKey(@RequestBody Map<String, String> params) {
        for (Map.Entry<String, String> entry : params.entrySet()) {
            ConfigUtil.set("website_map_key", entry.getKey(), entry.getValue());
        }
    }
}
